//
//  PermissionsSet.m
//  Lightthings
//
//  Created by Hero on 2024/9/18.
//

#import "PermissionsSet.h"
#import "JYPopContainerView.h"
#import "JYRoot.h"
#import "YYCategoriesMacro.h"
#import "PermissionsSetCell.h"
#import "LocationManager.h"
#import <Photos/Photos.h>
@interface PermissionsSet ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) JYPopContainerView *popView;

/* <#note#> */
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSArray * dataArr, * subDataArr;
/* <#note#> */
@property (nonatomic, assign) BOOL photolibrary, ble, location;
@end

@implementation PermissionsSet


- (instancetype)init {
    if (self = [super init]) {
        
        self.dataArr = @[SWString(@"Bluetooth permissions"), SWString(@"Camera permissions"),SWString(@"Location permissions"),SWString(@"Phone permissions"),SWString(@"Message permissions")];
        
        self.subDataArr = @[NSLocalizedStringFromTable(@"Used to scan, connect, and bind your devices", @"InfoPlist", nil),NSLocalizedStringFromTable(@"Used to use the watch to control entering and exiting the camera", @"InfoPlist", nil),NSLocalizedStringFromTable(@"Used to update weather information", @"InfoPlist", nil),NSLocalizedStringFromTable(@"Use the watch to control the answer and end of the call", @"InfoPlist", nil),NSLocalizedStringFromTable(@"Used to get your mobile phone messages and push them to the watch", @"InfoPlist", nil)];
        
        [self setupUI];
        [self bar];
        self.bar.titleLab.text = SWString(@"Authority management");
        
        [self requestAuthorization];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.tableView];
}

- (void)requestAuthorization {
    
    AVAuthorizationStatus camera = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (camera == AVAuthorizationStatusAuthorized) {
        self.photolibrary = YES;
    }else {
        self.photolibrary = NO;
    }
    FRQManagerState  bn = [FRQBluetoothManager shareManager].state;
    if (bn == FRQManagerStateUnknown || bn == FRQManagerStateUnsupported|| bn == FRQManagerStateUnauthorized) {
        self.ble = NO;
    }else {
        self.ble = YES;
    }
    
    self.location = [LocationManager getLocationEnable];
    
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PermissionsSetCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PermissionsSetCell" forIndexPath:indexPath];
    cell.nameLab.text = self.dataArr[indexPath.row];
    cell.subInfoLab.text = self.subDataArr[indexPath.row];
    
    if (indexPath.row == 1) {
        if (self.photolibrary) {
            [cell.setBtn setTitle:SWString(@"Turned On") forState:0];
            [cell.setBtn setTitleColor:jy_HexColor(@"0x666666") forState:0];
        }else {
            [cell.setBtn setTitle:SWString(@"Go to settings") forState:0];
            [cell.setBtn setTitleColor:[UIColor blueColor] forState:0];
            [cell.setBtn addTarget:self action:@selector(onclicklibrary) forControlEvents:UIControlEventTouchUpInside];
        }
    }else if (indexPath.row == 0) {
        if (self.ble) {
            [cell.setBtn setTitle:SWString(@"Turned On") forState:0];
            [cell.setBtn setTitleColor:jy_HexColor(@"0x666666") forState:0];
        }else {
            [cell.setBtn setTitle:SWString(@"Go to settings") forState:0];
            [cell.setBtn setTitleColor:[UIColor blueColor] forState:0];
            [cell.setBtn addTarget:self action:@selector(onclickBle) forControlEvents:UIControlEventTouchUpInside];
        }
    }else if (indexPath.row == 2) {
        if (self.location) {
            [cell.setBtn setTitle:SWString(@"Turned On") forState:0];
            [cell.setBtn setTitleColor:jy_HexColor(@"0x666666") forState:0];
        }else {
            [cell.setBtn setTitle:SWString(@"Go to settings") forState:0];
            [cell.setBtn setTitleColor:[UIColor blueColor] forState:0];
            [cell.setBtn addTarget:self action:@selector(onclicklibrary) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

- (void)onclicklibrary {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

}
- (void)onclickBle {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

}

- (void)dimiss
{
    [self.popView dissmiss];
}
- (void)show
{
    if (self.popView) {
        [self.popView removeFromSuperview];
    }
    JYPopContainerView *popView = [JYRoot popBottomView:self animation:YES maker:^(MASConstraintMaker * _Nonnull make) {
        kMakeLRV(0);
        kMakeBV(0);
//        kMakeHV(klSafeAreaEdgeInsets.bottom  + 410);
        kMakeHV(klSafeAreaEdgeInsets.bottom  + 270);
    } block:nil];
    [popView layoutSubviews];
    self.popView = popView;

    @weakify(popView);
    self.eventTransmissionBlock = ^(id  _Nonnull params, NSInteger tag) {
        @strongify(popView);
        if (tag == 0) {
            [popView dissmiss];
        }
    };

    [self.superview layoutIfNeeded];

}


- (NSString *)containerColor {
    return @"#FFFFFF";
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 340) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:@"PermissionsSetCell" bundle:nil] forCellReuseIdentifier:@"PermissionsSetCell"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedRowHeight = 40;
        } 
        _tableView.scrollsToTop =  NO;
        _tableView.bounces =  NO;
        if (@available(iOS 15, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
    }
    return _tableView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
